#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re

if len(sys.argv) != 2:
    print "invalid number of args: ./inline_replace.py filename"
    exit(-1)
filename = sys.argv[1]


lines = open(filename, "r").readlines()
if len(lines) == 0:
    exit(0)

message_start = False
keep_omitempty = False
for index, line in enumerate(lines):
    if not message_start:
        if re.search("type (\w+) struct {", line):
            message_start = True
            keep_omitempty = index > 0 and lines[index-1].find("// keep omitempty") != -1
    else:
        if line.startswith("XXX_") or line.startswith("}"):
            message_start = False
            keep_omitempty = False
        else:
            if not keep_omitempty:
                line = line.replace(",omitempty", "")
                lines[index] = line

with open(filename, "w") as fd:
    if lines[0].strip() != "//@generated":
        fd.write("//@generated\n")
    fd.writelines(lines)
